package services;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DataAccess.FileShareDA;
import DataAccess.LogThaoTacDA;
import DataAccess.NguoiDungDA;

import pojo.FileSharePOJO;
import pojo.LogThaoTacPOJO;
import pojo.NguoiDungPOJO;
import pojo.PairValue;
import util.ClientSender;
import util.XMLBuidler;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		LogThaoTacPOJO log=new LogThaoTacPOJO("phu","thao tac","ghi chu",1);
//		new LogThaoTacDA().insert(log);
		PrintWriter out=response.getWriter();
//		ArrayList<LogThaoTacPOJO> lstLog=new LogThaoTacDA().getByIdCay(1);
//		out.print(LogThaoTacPOJO.createXML(lstLog));
//		request.setCharacterEncoding("UTF-8");
//		response.setContentType("text/html; charset=UTF-8");
		String s=request.getParameter("s");
		out.print(s);
		System.out.print(s);
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	private void shareFile(ServletResponse response) throws IOException{
		PrintWriter out=response.getWriter();
		FileSharePOJO file=new FileShareDA().get(95);
		ArrayList<FileSharePOJO> lstFile=new ArrayList<FileSharePOJO>();
		lstFile.add(file);
		ArrayList<PairValue> params=new ArrayList<PairValue>();
		params.add(new PairValue(Protocol.CMD, Protocol.SHARE_FILE));	
		params.add(new PairValue(Protocol.NOI_DUNG, XMLBuidler.danhSachFile(lstFile)));
		NguoiDungPOJO nd=new NguoiDungDA().get("phu");
		
		try {
			ClientSender.send(nd.getDeviceID(), params);
			out.write("OK");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace(out);
		}
	}
}
